<?php
/**
 * 创建上传目录和设置权限
 */

// 创建必要的上传目录
$directories = [
    '../../../uploads',
    '../../../uploads/avatars',
    '../../../uploads/temp'
];

foreach ($directories as $dir) {
    if (!file_exists($dir)) {
        if (mkdir($dir, 0755, true)) {
            echo "✓ 创建目录成功: $dir\n";
        } else {
            echo "✗ 创建目录失败: $dir\n";
        }
    } else {
        echo "✓ 目录已存在: $dir\n";
    }
}

// 创建 .htaccess 文件保护上传目录
$htaccessContent = '# 禁止直接访问 PHP 文件
<Files "*.php">
    Order Deny,Allow
    Deny from all
</Files>

# 允许图片文件访问
<FilesMatch "\.(jpg|jpeg|png|gif|webp)$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

# 设置缓存头
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
</IfModule>';

$htaccessFile = '../../../uploads/.htaccess';
if (!file_exists($htaccessFile)) {
    if (file_put_contents($htaccessFile, $htaccessContent)) {
        echo "✓ 创建 .htaccess 文件成功\n";
    } else {
        echo "✗ 创建 .htaccess 文件失败\n";
    }
} else {
    echo "✓ .htaccess 文件已存在\n";
}

echo "\n上传目录初始化完成！\n";
?>